<?php
/**
 * 唯一入口
 */

    class TIndex
    {
    	public $sys = array();
    	private $main_app = array();
    	static public $request = array();
    	
    	function __construct(){
    		//加载框架基本配置
            $this->loadConfig();
            //分析请求
            $this->httprequest();
            //路由器
            $this->routeMap();
            //应用
            $tapp = $this->createApp();
            //layout
            $this->layOut($tapp);
            //out
            $this->out($tapp);
    	}
    	
    	
        function loadConfig(){
            require_once 'config.inc.php';
            /**
             * 自定义拦截
             */	
        }
        
        function httpRequest(){
        	//处理sys_uri
        	if ($_SERVER['REQUEST_URI'] ){
        		$url = $_SERVER['REQUEST_URI'];
        		//兼容开头使用&的方式
        	    $url =  preg_replace('/^([^?&]*)&(.*)$/', '\1?\2', $url);
        		$url_array = parse_url($url);
        	    if ($url_array){
        	    	$get = array();
        	    	if (isset($url_array['query'])){
        	    	    parse_str($url_array['query'], $get);
        	    	}
	        	    $_GET['sys_uri'] = $url_array['path'];
	        	    $_GET = array_merge($_GET,$get);
	        	    $_REQUEST = array_merge($_REQUEST,$_GET);
        	    }
        	}
        	
        	//获取sys公用参数
        	foreach ($_REQUEST as $key=>$value){
                if (substr($key,0,4) === 'sys_'){
                    $this->sys[$key] = $value;
                }        	   
        	}
        	
        	self::$request = array_merge( $_GET ,$_POST);
            /**
             * 自定义拦截
             */
        	
        	
        	//print_r($this->sys);exit;
        }
        
        /**
         *  uri使用三层模式
         *  如果api为空，则默认 request_method
         *  app的信息放在 $_REQUEST['sys_route']中
         */
        function routeMap(){
        	//sys_uri，必须是get方式的。
        	if (empty($_GET['sys_uri']) || $_GET['sys_uri'] == '/'){
        	   $this->sys['sys_uri'] = TAPP_INDEX;
        	}
        	/*自定义拦截*/
        	
            $this->main_app = TRestful::parseUri($this->sys['sys_uri']);
            if (empty($this->main_app['api'])){
                $this->main_app['api'] = strtolower($_SERVER['REQUEST_METHOD']);
            }
        }
        
        function createApp(){
        	$params = array(&$this->main_app,&self::$request,&$this->sys);
        	TPlugin::start('createApp',$params);
        	/*拦截restful的请求*/
        	if ($this->main_app['folder'] === 'restful'){
        	   require_once TFRAME_DIR.'task/DTask.server.php';
               $app = new DTaskServer();
               return true;
        	}
        	
        	if ($_REQUEST['sys_uri'] == TTESTCODE_HTTP){
        	   include TT_DIR.'testcode/test.code.php';
        	   return true;
        	}
        	
        	/*正常app*/
        	return TApp::start($this->main_app,self::$request,$this->sys);
        }
        
        /**
         * 
         * @param $app
         */
        function layOut($tapp){
        	if ($tapp instanceof TApp){
        	   $ob = $tapp->ob;
        	   $app_config = $tapp->app_config;
        	   if (isset($app_config['lay_out']) && $app_config['lay_out']){
        	   	   gdata('TAPP_LAYOUT',$tapp);
        	       $lay_out = TApp::start($app_config['lay_out'],self::$request,$tapp->sys);
        	       $ob = $lay_out->ob;
        	   }
        	   echo $ob;
        	}
        	else {
        	   echo 'TApp error';
        	}
        }
        
        /**
         * 输出的补充
         * @param $tapp
         */
        function out($tapp){
            if ($tapp instanceof TApp){
               $app_config = $tapp->app_config;
               //print_r($app_config);
               if ($app_config['debug_log']){
                  TDebug::printDebug(0,1,$app_config['debug_log']);
               }
               else if ($app_config['ajax']){
                  TDebug::printDebug(0,1,'ajax_temp');
               }else {
                  TDebug::printDebug(1,0,'');
               }
            }
        }
    	
    }
header('Content-Type: text/html; charset=utf-8');    
    new TIndex();